//
//  WBNavigationViewController.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/5/18.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit

class WBNavigationViewController: UINavigationController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        /// - 隐藏默认的 NavigationBar
        navigationBar.isHidden = true 
    }

    /// - 重写push方法，只要push都会调用此方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    
        /// - 如果不是栈底控制器才需隐藏，根控制器不需要隐藏
        if childViewControllers.count > 0 {/// - 大于0就不是根控制器
            /// - 隐藏tabBar
           viewController.hidesBottomBarWhenPushed = true
            /// - 判断控制器类型
            if let vc = viewController as? WBBaseViewController {
                var title = "返回"
                /// - 如果是第一个子控制器，就显示子控制器的名字，否则就显示返回
                if childViewControllers.count == 1 {
                    /// - title 显示首页的标题 ，
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, titleColorNormal: UIColor.purple, titleColorHigh: UIColor.orange, target: self, action: #selector(popToParent),isBack:true)
            }
            
            
            
            
        }
        /// - 只能写在下面，写上面无效
         super.pushViewController(viewController, animated: true)
        
    }
    
    func popToParent()  {
        
        popViewController(animated: true)
    }

}
